package org.jeecg.modules.wlhy.bill.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.wlhy.bill.dto.HyDriverBillDTO;
import org.jeecg.modules.wlhy.bill.entity.HyDriverBill;
import org.jeecg.modules.wlhy.bill.mapper.HyDriverBillMapper;
import org.jeecg.modules.wlhy.bill.service.IHyDriverBillService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.Map;

/**
 * @Description: hy_driver_bill
 * @Author: jeecg-boot
 * @Date:   2021-01-20
 * @Version: V1.0
 */
@Service
public class HyDriverBillServiceImpl extends ServiceImpl<HyDriverBillMapper, HyDriverBill> implements IHyDriverBillService {

    @Override
    public Map<String, String> getTotalAmtByDriverId(String driverId,String payChannel) {
        return baseMapper.getTotalAmtByDriverId(driverId,payChannel);
    }

    @Override
    public Map<String, String> getTotalAmtByDriverIdForHsyh(String driverId, String payChannel) {
        return baseMapper.getTotalAmtByDriverIdForHsyh(driverId, payChannel);
    }

    @Override
    public Map<String, String> getTotalAmtByDriverIdForPingAn(String driverId, String payChannel) {
        return baseMapper.getTotalAmtByDriverIdForPingAn(driverId, payChannel);
    }

    @Override
    public HyDriverBill getPreBillByShippingId(String shipping_id) {
        return  this.getOne(new QueryWrapper<HyDriverBill>()
                .eq("shipping_id", shipping_id)
                .eq("bill_type", "6")
        );
    }

    @Override
    public HyDriverBill getBillByShippingId(String shipping_id) {
        return  this.getOne(new QueryWrapper<HyDriverBill>()
                .eq("shipping_id", shipping_id)
                .and(qw->qw.eq("bill_type", "1")
                           .or()
                        .eq("bill_type", "3")
                )
        );
    }

    @Override
    public int getDriverTxCountsByDate(String driverId ,Date date) {
        QueryWrapper<HyDriverBill> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id", driverId);
        if (date != null) {
            queryWrapper.apply("DATE_FORMAT(create_time,'%Y-%m-%d')={0}", DateUtil.format(date, "yyyy-MM-dd"));
        }
        return this.count(queryWrapper);
    }

    @Override
    public IPage<HyDriverBillDTO> getPageVo(Page<HyDriverBillDTO> page, QueryWrapper<HyDriverBillDTO> queryWrapper) {
        return baseMapper.pageList(page, queryWrapper);
    }
}
